﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Api;
using ZBJF.Warranty.Common.Enums.InsuranceOrder;
using ZBJF.Warranty.Model.CommonObject.CardInfo;
using ZBJF.Warranty.PinanAPI.Model;

namespace ZBJF.Warranty.WebProvider.InsuranceProducts
{
    public class InsuranceProductOrderCardProvider
    {
        private static InsuranceProductOrderCardProvider _instance;
        private static readonly object Lock = new object();

        public static InsuranceProductOrderCardProvider GetInstance()
        {
            if (_instance != null) return _instance;
            lock (Lock)
            {
                if (_instance == null)
                {
                    return _instance = new InsuranceProductOrderCardProvider();
                }
            }
            return _instance;
        }

        /// <summary>
        /// 验证卡密的合法性，根据卡密验证自己系统是否有效，再去平安验证是否有效
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="password"></param>
        /// <param name="cardInfoReturnEntity"></param>
        /// <returns></returns>
        public bool ValidateCardInfo(string cardNo, string password, ref CardInfoReturnEntity cardInfoReturnEntity)
        {
            cardInfoReturnEntity=new CardInfoReturnEntity();
            var cardInfoList = ServiceFactory.InsuranceProductOrderCardServiceInstance.Load(cardNo,
                    password);
            foreach (var info in cardInfoList)
            {
                if (ValidateCardInfo(info.InsuranceProductId, info.CardNo, info.CardPassword, ref cardInfoReturnEntity))
                {
                    return true;
                }
            }
            var resEntity = new PinanValidateCardData();
            var flag = PinanAPI.InsuranceService.ValidateSelfServerCard(cardNo, password, ref resEntity);
            cardInfoReturnEntity.InsuranceCardType=InsuranceCardType.Pinan;
            cardInfoReturnEntity.PolicyPeriod = resEntity.policyPeriod;
            cardInfoReturnEntity.PolicyPeriodUnit = resEntity.policyPeriodUnit;
            cardInfoReturnEntity.WaitDays = resEntity.waitDays;
            return flag;
        }
        /// <summary>
        /// 自己系统验证 卡密的合法性
        /// </summary>
        /// <param name="insuranceProductId"></param>
        /// <param name="cardNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateCardInfo(long insuranceProductId, string cardNo, string password, ref CardInfoReturnEntity cardInfoReturnEntity)
        {
            var product = ServiceFactory.InsuranceProductsServiceInstance.LoadByID(insuranceProductId);
            if (product != null)
            {
                //cardInfoReturnEntity.PolicyPeriod = product.PolicyPeriod.Value;
                //cardInfoReturnEntity.PolicyPeriodUnit = product.PolicyPeriodUnit.Value;
                //cardInfoReturnEntity.WaitDays = product.WaitDays.Value;
            }
            else
            {
                //cardInfoReturnEntity.PolicyPeriod = 1;
                //cardInfoReturnEntity.PolicyPeriodUnit = (byte)InsuranceOrderPeriodUnit.Year;
                //cardInfoReturnEntity.WaitDays = 7;
            }
            cardInfoReturnEntity.InsuranceCardType = InsuranceCardType.ZBJF;
            return ServiceFactory.InsuranceProductOrderCardServiceInstance.ValidateCardPassword(insuranceProductId, cardNo, password);
        }
        /// <summary>
        /// 激活自己系统卡密
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ActivatedCardInfo(string cardNo, string password)
        {
            var cardInfoList = ServiceFactory.InsuranceProductOrderCardServiceInstance.Load(cardNo,
                    password);
            CardInfoReturnEntity cardInfoReturnEntity=new CardInfoReturnEntity();
            foreach (var info in cardInfoList)
            {
                if (ValidateCardInfo(info.InsuranceProductId, info.CardNo, info.CardPassword, ref cardInfoReturnEntity))
                {
                    ServiceFactory.InsuranceProductOrderCardServiceInstance.UseCard(info.InsuranceProductId, cardNo, password);
                }
            }
            return false;
        }
        /// <summary>
        /// 根据基础产品 激活自己系统卡密
        /// </summary>
        /// <param name="insuranceProductId"></param>
        /// <param name="cardNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ActivatedCardInfo(long insuranceProductId, string cardNo, string password)
        {
            return ServiceFactory.InsuranceProductOrderCardServiceInstance.UseCard(insuranceProductId, cardNo, password);
        }
    }
}
